package com.winai.launcher.view.set;

import android.content.Context;
import android.content.Intent;
import android.net.DhcpInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.winai.launcher.R;
import com.winai.launcher.base.BaseActivity;
import com.winai.launcher.common.GlobalPublisher;
import com.winai.launcher.manager.NetworkManager;
import com.winai.launcher.manager.WifiAutoConnectManager;
import com.winai.launcher.override.dialog_global.GlobalDialog;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 作者：康少
 * 时间：2019/11/22 0022
 * 说明：设置wifi模式、IP、网关等
 */
public class SetWifiDetailActivity extends BaseActivity {
    private Context mContext = this;

    @BindView(R.id.str_title)
    TextView strTitle;
    @BindView(R.id.et_ip)
    EditText etIp;
    @BindView(R.id.et_gateway)
    EditText etGateway;
    @BindView(R.id.et_DNS)
    EditText etDNS;
    @BindView(R.id.tv_IPModel)
    TextView tvIPModel;
    @BindView(R.id.tv_wifiModel)
    TextView tvWifiModel;

    private WifiAutoConnectManager wifiAutoConnectManager;
    private WifiManager mWifiManager;
    private String wifiSSID = "";
    private String wifiBSSID = "";

    private GlobalDialog.Builder tipDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_wifi_detail);
        ButterKnife.bind(this);
        start();
    }

    private void start() {
        initObject();
        toggleModel();
        initData();
    }

    private void initObject() {
        mWifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
        wifiAutoConnectManager = WifiAutoConnectManager.newInstance(mWifiManager);
        wifiSSID = getIntent().getStringExtra("wifiSSID");
        wifiBSSID = getIntent().getStringExtra("wifiBSSID");

        tipDialog = new GlobalDialog.Builder(mContext)
                .setTitle("是否忽略此网络")
                .setContent("忽略此网络后，设备将断开此网络连接，并清空该网络密码！")
                .setConfirmText("忽略")
                .setCancelText("取消")
                .setConfirmGradient(true)
                .setOnConfirmClickListener(new GlobalDialog.OnConfirmClickListener() {
                    @Override
                    public void onClick(GlobalDialog mGlobalDialog, View v) {
                        // 忽略WiFi，忘记网络
                        wifiAutoConnectManager.removeWifiBySsid(wifiSSID);
                        NetworkManager.getInstance(mContext).setConnectedWifi(null);
                        GlobalPublisher.Notify(GlobalPublisher.CHANGE_WIFI, 0);
                        finish();
                    }
                });
    }

    private void initData() {
        DhcpInfo dhcpInfo = mWifiManager.getDhcpInfo();
        int dns1 = dhcpInfo.dns1;
        String gateway = WifiAutoConnectManager.getGateway();
        String ipAddress = WifiAutoConnectManager.getIpAddress();
        etIp.setText(ipAddress);
        etGateway.setText(gateway);
        etDNS.setText(intToIp(dns1));
    }

    public String intToIp(int i) {
        return ((i) & 0xFF) + "." +
                ((i >> 8) & 0xFF) + "." +
                ((i >> 16) & 0xFF) + "." +
                (i >> 24 & 0xFF);
    }

    /**
     * 说明：切换模型
     */
    private void toggleModel() {
        String s = tvWifiModel.getText().toString();
        if (s.equals("手动")) {
            tvWifiModel.setText("自动");
            tvIPModel.setText("自动");
        } else {
            tvWifiModel.setText("手动");
            tvIPModel.setText("手动");
        }
        toggleEditText(s);
    }

    /**
     * 说明：切换输入框可用不可用
     */
    private void toggleEditText(String model) {
        etIp.setClickable(model.equals("自动"));
        etIp.setFocusable(model.equals("自动"));
        etIp.setFocusableInTouchMode(model.equals("自动"));
        etGateway.setClickable(model.equals("自动"));
        etGateway.setFocusable(model.equals("自动"));
        etGateway.setFocusableInTouchMode(model.equals("自动"));
        etDNS.setClickable(model.equals("自动"));
        etDNS.setFocusable(model.equals("自动"));
        etDNS.setFocusableInTouchMode(model.equals("自动"));
    }

    @OnClick({R.id.iv_back, R.id.iv_home, R.id.tv_ignore, R.id.tv_wifiModel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.iv_home:
                //模拟HOME
                Intent it = new Intent(Intent.ACTION_MAIN);
                it.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                it.addCategory(Intent.CATEGORY_HOME);
                mContext.startActivity(it);
                break;
            case R.id.tv_wifiModel:
                /*toggleModel();*/
                break;
            case R.id.tv_ignore:
                tipDialog.show();
                break;
        }
    }
}
